/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.os;

import java.util.*;
import java.util.zip.*;
import java.io.*;
import javax.servlet.*;
import bw.util.*;

public class FileObjectHandler extends PersistentObjectHandler 
    implements PersistentObjectReader, PersistentObjectWriter
{
    private File _dataDir = null;

    public FileObjectHandler ( ServletContext context )
    {
        String dataDirStr = context.getInitParameter( "objectStore.file.dataDir" );

        _dataDir = new File( context.getRealPath( dataDirStr ) );

        if ( !_dataDir.exists() )
        {
            Log.getInstance().write( "FileObjectStore: DATA DIR (" + 
                                     _dataDir.getAbsolutePath() + 
                                     ") DOES NOT EXIST!!!" );
        }

        Log.getInstance().write( "FileObjectHandler: " + _dataDir.getAbsolutePath() );
    }

    public void write ( String key, Object obj )
        throws Exception
    {
        File f = makeFile( key ); 
        
        //Log.getInstance().write( "Writing object for: " + f.getAbsolutePath() );

        ObjectOutputStream oos = getObjectOutputStream( new FileOutputStream( f ) );

        oos.writeObject( obj );

        oos.close();
    }

    public void erase ( String key )
        throws Exception
    {
        File f = makeFile( key );
        f.delete();
    }

    /*    protected Set keys ()
        throws Exception
    {
        HashSet keys = new HashSet();

        File [] files = _dataDir.listFiles( new ObjectFileFilter() );
        
        if ( files != null )
        {
            for ( int i = 0; i < files.length; i++ )
            {
                keys.add( extractKey( files[i].getName() ) );
            }
        }

        return keys;
        }*/
    
    public Object read ( String key )
        throws Exception
    {
        Object obj = null;

        if ( key != null )
        {
            File f = makeFile( key );
                
            if ( f.exists() )
            {
                //Log.getInstance().write( "Recovering object for: " + f.getAbsolutePath() );
                
                obj = getObjectFromStream( new FileInputStream( f ) );
            }
        }

        return obj;
    }

    public void shutdown () throws Exception
    {
        //nothing
    }

    private File makeFile ( String key )
    {
        return new File( _dataDir, makeFilename( key ) );
    }

    class ObjectFileFilter implements FilenameFilter
    {
        public boolean accept ( File dir, String name )
        {
            return ( dir.equals( _dataDir) && name.endsWith( FILE_EXT ) );
        }
    }

    public static void main ( String [] args )
    {
        if ( args.length != 1 )
        {
            System.out.println( "FileObjectStore <file>" );
            System.exit( 0 );
        }

        try
        {
            File f = new File( args[0] );
            FileInputStream fin = new FileInputStream( f );
            GZIPInputStream gzin = new GZIPInputStream( fin );
            ObjectInputStream oin = new ObjectInputStream( gzin );
            Object obj = oin.readObject();
            oin.close();

            System.out.println( obj );
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }

}